package cmd

///global mem

import (
	"github.com/BurntSushi/toml"
	"github.com/dwbmio/ding-cli/pkg"
	"github.com/dwbmio/ding-cli/pkg/static"
	"github.com/wonderivan/logger"
	"os"
	"path"
)

type ProjConf struct {
	RootPath string   `toml:"root_path"`
	Servers  []string `toml:"servers"`
	Platform string   `toml:"platform"`
}

var requireP = []string{
	"root_path",
	"servers",
}

//variable from flags
var (
	platform string
	srcPath  string
	rootPath string
	servers  []string
)

//variable in runtime
var (
	nginxConf string
)

func CheckProjConf(pwd string) (*ProjConf, error) {
	ret := &ProjConf{}

	f := path.Join(pwd, static.DingProjConfig)
	_, err := os.Stat(f)
	if os.IsNotExist(err) {
		return nil, pkg.NewDError(103, f)
	}
	conf, err := toml.DecodeFile(f, ret)
	if err != nil {
		logger.Error(err.Error())
		return ret, pkg.NewDError(101)
	}
	for _, p := range requireP {
		if !conf.IsDefined(p) {
			logger.Error("not find require params named<%s>", p)
			return ret, pkg.NewDError(102)
		}
	}
	return ret, err
}
